const fs = require('fs'),
    join = require('path').join
/**
 * @function 获取本机IP地址
 * @return {type} {description}
 */
function getIPAdress () {
    var interfaces = require('os').networkInterfaces()
    for (var devName in interfaces) {
        var iface = interfaces[devName]
        for (var i = 0; i < iface.length; i++) {
            var alias = iface[i]
            if (alias.family === 'IPv4' && alias.address !== '127.0.0.1' && !alias.internal) {
                return alias.address
            }
        }
    }
}

/**
 * 获取目录下所有文件的地址
 * @param startPath  起始目录文件夹路径
 * @returns {Array}
 */
function getFilesPath (startPath) {
    let result = []
    function finder (path) {
        let files = fs.readdirSync(path)
        files.forEach((val, index) => {
            let fPath = join(path, val)
            let stats = fs.statSync(fPath)
            if (stats.isDirectory()) finder(fPath)
            if (stats.isFile()) result.push(fPath)
        })
    }
    finder(startPath)
    return result
}

module.exports = {
    getIPAdress: getIPAdress,
    getFilesPath: getFilesPath
}
